package cn.unknowpupil.controller.system;

import cn.unknowpupil.common.core.annotation.Log;
import cn.unknowpupil.common.core.enums.BusinessType;
import cn.unknowpupil.common.core.resopnse.ResultResponse;
import cn.unknowpupil.domain.system.SysNotice;
import cn.unknowpupil.service.system.ISysNoticeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created at 2020-11-03 by  w10g <br>
 * Initialize SysNotice  ... <br>
 *  通知公告表 视图浏览层处理
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Api(tags = {"系统-通知公告表模块" }, value = "SysNotice")
@RestController
@RequestMapping(value = "admin/sys-notice")
public class SysNoticeController {

    @Resource
    private ISysNoticeService sysNoticeService;

    /**
     * 新增
     *
     * @param sysNotice {@link SysNotice}  通知公告表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：新增")
    @Log(title = "create", businessType = BusinessType.INSERT)
    @PostMapping("create")
    public ResultResponse create(@Validated @RequestBody SysNotice sysNotice) {


        // 业务逻辑
        boolean created = sysNoticeService.create(sysNotice);
        if (created) {
             return ResultResponse.success("创建成功");
        }else{
             return ResultResponse.buildFailure("创建失败");
        }

    }

    /**
     * 修改
     *
     * @param sysNotice {@link SysNotice}  通知公告表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：修改")
    @Log(title = "update", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    public ResultResponse update(@Validated @RequestBody SysNotice sysNotice) {

        // 业务逻辑
        boolean update = sysNoticeService.update(sysNotice);
        if (update) {
             return ResultResponse.success("修改成功");
        }else{
             return ResultResponse.buildFailure("修改失败");
        }

    }

    /**
     * 获取
     *
     * @param id {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：获取")
    @GetMapping("get/{id}")
    public ResultResponse get(@PathVariable("id") Long id) {
        SysNotice sysNotice = sysNoticeService.info(id);
        return ResultResponse.buildSuccess(sysNotice);
    }

    /**
     * 删除
     *
     * @param ids {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：删除")
    @Log(title = "remove", businessType = BusinessType.DELETE)
    @DeleteMapping("remove/{ids}")
    public ResultResponse remove(@PathVariable("ids") Long[] ids) {
        // 业务逻辑
        boolean deleted  = sysNoticeService.delete(ids);
        if(deleted){
            return ResultResponse.success("删除成功");
        }else{
            return ResultResponse.buildFailure("删除失败");
        }
    }

    /**
        * 分页
        *
        * @param current {@code int} 页码
        * @param size    {@code int} 笔数
        * @return {@link ResultResponse}
        */
    @ApiOperation("系统：分页")
    @GetMapping("page/{current}/{size}")
    public ResultResponse<IPage<SysNotice>> page(@PathVariable("current") int current, @PathVariable("size") int size, @ModelAttribute SysNotice sysNotice) {
            IPage<SysNotice> page = sysNoticeService.page(current, size, sysNotice);
            return ResultResponse.buildSuccess(page);
    }
}
